package org.example.restudying2.service;

import org.example.restudying2.mapper.CustomerMapper;
import org.example.restudying2.pojo.Customer;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
@Service
public class customerService {
    @Resource
    CustomerMapper customerMapper;
    public Map<String, Object> login(String customername, String password){
        Map<String, Object> map = new HashMap<>();
        Customer customer = this.customerMapper.login(customername);
        if(customername == null ){
            map.put("code", 556);
            map.put("msg", "用户姓名不能为空");
            return map;
        }
        if(password == null ){
            map.put("code", 557);
            map.put("msg", "用户密码不能为空");
            return map;
        }

        byte[] bytes = password.getBytes(StandardCharsets.UTF_8);
        password = DigestUtils.md5DigestAsHex(bytes);
        System.out.println(customer.getPassword()+" "+password);
        if(customer.getPassword().equals(password)){
            map.put("code",200);
            map.put("msg","登录成功");
            map.put("customer",customer);
        }else{
            map.put("code",590);
            map.put("msg","用户名或者密码错误");
        }
        return map;
    }

    public Map<String, Object> register (String customername, String password, String email){
        Map<String, Object> map = new HashMap<>();

        byte[] bytes = password.getBytes(StandardCharsets.UTF_8);
        password = DigestUtils.md5DigestAsHex(bytes);

        map.put("code", 200);
        map.put("msg","登录成功");
        map.put("customer",this.customerMapper.register(customername,password,email));
        return map;
    }
}
